﻿#include "CalcTemplateTimeline.h"

void CalcStartTimeFunc(std::vector<JsonVideoClip::VideoClipInfo*> VideoInfo, std::string FuncString, float* OriginalTime)
{
    std::regex FuncPattern(R"(\$([a-zA-Z]+)_([0-9]+)_([a-zA-Z]+)\$([-+]?[0-9]+(?:\.[0-9]+)?))");
    std::regex SymbolPattern(R"(([-+]?)([0-9]+(?:\.[0-9]+)?))");
    std::smatch FuncMatches;
    
    if(std::regex_search(FuncString, FuncMatches, FuncPattern))
    {
        if(FuncMatches[1] == "video")
        {
            int Index = atoi(FuncMatches[2].str().c_str());
            float PositionTimeRef = VideoInfo[Index]->PositionTime;
            float BeginTimeRef = VideoInfo[Index]->BeginTime;
            float EndTimeRef = VideoInfo[Index]->EndTime;
            float ZoomRateRef = VideoInfo[Index]->ZoomRate;
                
            if(FuncMatches[3] == "start")
            {
                *OriginalTime = PositionTimeRef;
            }
            else if(FuncMatches[3] == "finish")
            {
                *OriginalTime = PositionTimeRef + (EndTimeRef - BeginTimeRef) * ZoomRateRef;
            }

            std::smatch SymbolMatches;
            std::string SymbolNumber = FuncMatches[4];
            if(std::regex_search(SymbolNumber, SymbolMatches, SymbolPattern))
            {
                float Number = atof(SymbolMatches[2].str().c_str());
                if(SymbolMatches[1] == "-")
                {
                    *OriginalTime = *OriginalTime - Number;
                }
                else if(SymbolMatches[1] == "+")
                {
                    *OriginalTime = *OriginalTime + Number;
                }
            }
        }
        else
        {
            if(!FuncString.empty())
            {
                std::cout << "Json Value of Key: 'timeline_start_function is' Vaild!" << std::endl;   
            }
        }
    }
}

void CalcFinishTimeFunc(std::vector<JsonVideoClip::VideoClipInfo*> VideoInfo, float CurrentPositionTime, float SrcDurationTime, std::string FuncString, float* OriginalZoomRate)
{
    std::regex FuncPattern(R"(\$([a-zA-Z]+)_([0-9]+)_([a-zA-Z]+)\$([-+]?[0-9]+(?:\.[0-9]+)?))");
    std::regex SymbolPattern(R"(([-+]?)([0-9]+(?:\.[0-9]+)?))");
    std::smatch FuncMatches;
    
    if(std::regex_search(FuncString, FuncMatches, FuncPattern))
    {
        if(FuncMatches[1] == "video")
        {
            int Index = atoi(FuncMatches[2].str().c_str());
            float PositionTimeRef = VideoInfo[Index]->PositionTime;
            float BeginTimeRef = VideoInfo[Index]->BeginTime;
            float EndTimeRef = VideoInfo[Index]->EndTime;
            float ZoomRateRef = VideoInfo[Index]->ZoomRate;
            float FinishTimeRef = PositionTimeRef + (EndTimeRef - BeginTimeRef) * ZoomRateRef;
            float DstFinishTime = CurrentPositionTime + SrcDurationTime;
                
            if(FuncMatches[3] == "start")
            {
                DstFinishTime = PositionTimeRef;
            }
            else if(FuncMatches[3] == "finish")
            {
                DstFinishTime = FinishTimeRef;
            }

            std::smatch SymbolMatches;
            std::string SymbolNumber = FuncMatches[4];
            if(std::regex_search(SymbolNumber, SymbolMatches, SymbolPattern))
            {
                float Number = atof(SymbolMatches[2].str().c_str());
                if(SymbolMatches[1] == "-")
                {
                    DstFinishTime = DstFinishTime - Number;
                }
                else if(SymbolMatches[1] == "+")
                {
                    DstFinishTime = DstFinishTime + Number;
                }
            }
            float DstDurationTime = DstFinishTime - CurrentPositionTime;
            *OriginalZoomRate = DstDurationTime / SrcDurationTime;
        }
        else
        {
            if(!FuncString.empty())
            {
                std::cout << "Json Value of Key: 'tiemline_finish_function is' Vaild!" << std::endl;
            }
        }
    }
}

void CalcZoomRateFunc(std::vector<JsonVideoClip::VideoClipInfo*> VideoInfo, std::string FuncString, float* OriginalZoomRate)
{
    std::regex FuncPattern(R"(\$([a-zA-Z]+)_([0-9]+)_([a-zA-Z]+)\$([-+]?[0-9]+(?:\.[0-9]+)?))");
    std::regex SymbolPattern(R"(([-+]?)([0-9]+(?:\.[0-9]+)?))");
    std::smatch FuncMatches;
    
    if(std::regex_search(FuncString, FuncMatches, FuncPattern))
    {
        if(FuncMatches[1] == "video")
        {
            int Index = atoi(FuncMatches[2].str().c_str());
            *OriginalZoomRate = VideoInfo[Index]->ZoomRate;
            
            std::smatch SymbolMatches;
            std::string SymbolNumber = FuncMatches[4];
            if(std::regex_search(SymbolNumber, SymbolMatches, SymbolPattern))
            {
                float Number = atof(SymbolMatches[2].str().c_str());
                if(SymbolMatches[1] == "-")
                {
                    *OriginalZoomRate = *OriginalZoomRate - Number;
                }
                else if(SymbolMatches[1] == "+")
                {
                    *OriginalZoomRate = *OriginalZoomRate + Number;
                }
            }
        }
    }
    else
    {
        if(!FuncString.empty())
        {
            std::cout << "Json Value of Key: 'tiemlien_zoom_function is' Vaild!" << std::endl;
        }
    }
}

void TemplateTimeLine::ResetTemplateVideoClip(const JsonVideoClip* const& VideoJsonData)
{
    for(JsonVideoClip::VideoClipInfo* VideoClipInfoItem : VideoJsonData->JsonVideoClipArray)
    {
        std::string StartFuncString = VideoClipInfoItem->StartTimeFunc;
        std::string FinishFuncString = VideoClipInfoItem->FinishTimeFunc;
        std::string ZoomRateFuncString = VideoClipInfoItem->ZoomRateFunc;
        float CurrentBeginTime = VideoClipInfoItem->BeginTime;
        float CurrentEndTime = VideoClipInfoItem->EndTime;
        
        CalcZoomRateFunc(VideoJsonData->JsonVideoClipArray, ZoomRateFuncString, &VideoClipInfoItem->ZoomRate);
        CalcStartTimeFunc(VideoJsonData->JsonVideoClipArray, StartFuncString, &VideoClipInfoItem->PositionTime);
        
        float SrcDurationTime = CurrentEndTime - CurrentBeginTime;      //不乘以CurrentZoomRate(当前的缩放率)，为了对齐模板，时间函数计算的缩放优先于客户设置的缩放
        CalcFinishTimeFunc(VideoJsonData->JsonVideoClipArray, VideoClipInfoItem->PositionTime, SrcDurationTime, FinishFuncString, &VideoClipInfoItem->ZoomRate);
    }
}

void TemplateTimeLine::ResetTemplateAudioClip(const JsonVideoClip& VideoJsonData, const JsonAudioClip* const& AudioJsonData)
{
    for(JsonAudioClip::AudioClipInfo* AudioClipInfoItem : AudioJsonData->JsonAudioClipArray)
    {
        std::string StartFuncString = AudioClipInfoItem->StartTimeFunc;
        std::string FinishFuncString = AudioClipInfoItem->FinishTimeFunc;
        std::string ZoomRateFuncString = AudioClipInfoItem->ZoomRateFunc;
        float CurrentBeginTime = AudioClipInfoItem->BeginTime;
        float CurrentEndTime = AudioClipInfoItem->EndTime;

        CalcZoomRateFunc(VideoJsonData.JsonVideoClipArray, ZoomRateFuncString, &AudioClipInfoItem->ZoomRate);
        CalcStartTimeFunc(VideoJsonData.JsonVideoClipArray, StartFuncString, &AudioClipInfoItem->PositionTime);

        float SrcDurationTime = CurrentEndTime - CurrentBeginTime;
        CalcFinishTimeFunc(VideoJsonData.JsonVideoClipArray, AudioClipInfoItem->PositionTime, SrcDurationTime, FinishFuncString, &AudioClipInfoItem->ZoomRate);
    }
}

void TemplateTimeLine::ResetTemplateVideoEffectClip(const JsonVideoClip& VideoJsonData, const JsonVideoEffectClip* const& VideoEffectJsonData)
{
    for(JsonVideoEffectClip::VideoEffectClipInfo* VideoClipInfoItem : VideoEffectJsonData->JsonVideoEffectClipArray)
    {
        std::string StartFuncString = VideoClipInfoItem->StartTimeFunc;
        std::string FinishFuncString = VideoClipInfoItem->FinishTimeFunc;
        std::string ZoomRateFuncString = VideoClipInfoItem->ZoomRateFunc;
        float CurrentBeginTime = VideoClipInfoItem->BeginTime;
        float CurrentEndTime = VideoClipInfoItem->EndTime;

        CalcZoomRateFunc(VideoJsonData.JsonVideoClipArray, ZoomRateFuncString, &VideoClipInfoItem->ZoomRate);
        CalcStartTimeFunc(VideoJsonData.JsonVideoClipArray, StartFuncString, &VideoClipInfoItem->PositionTime);

        float SrcDurationTime = CurrentEndTime - CurrentBeginTime;
        CalcFinishTimeFunc(VideoJsonData.JsonVideoClipArray, VideoClipInfoItem->PositionTime, SrcDurationTime, FinishFuncString, &VideoClipInfoItem->VideoLenghtRate);
    }
}

void TemplateTimeLine::ResetTemplateMaskEffectClip(const JsonVideoClip& VideoJsonData, const JsonMaskEffect* const& MaskEffectJsonData)
{
    for(JsonMaskEffect::MaskEffectInfo* MaskClipInfoItem : MaskEffectJsonData->JsonMaskEffectArray)
    {
        std::string StartFuncString = MaskClipInfoItem->StartTimeFunc;
        std::string FinishFuncString = MaskClipInfoItem->FinishTimeFunc;
        std::string ZoomRateFuncString = MaskClipInfoItem->ZoomRateFunc;
        float CurrentBeginTime = MaskClipInfoItem->BeginTime;
        float CurrentEndTime = MaskClipInfoItem->EndTime;

        CalcZoomRateFunc(VideoJsonData.JsonVideoClipArray, ZoomRateFuncString, &MaskClipInfoItem->ZoomRate);
        CalcStartTimeFunc(VideoJsonData.JsonVideoClipArray, StartFuncString, &MaskClipInfoItem->PositionTime);

        float SrcDurationTime = CurrentEndTime - CurrentBeginTime;
        CalcFinishTimeFunc(VideoJsonData.JsonVideoClipArray, MaskClipInfoItem->PositionTime, SrcDurationTime, FinishFuncString, &MaskClipInfoItem->ZoomRate);
    }
}

void TemplateTimeLine::ResetTemplateImageEffectClip(const JsonVideoClip& VideoJsonData, const JsonImageEffect* const& ImageEffectJsonData)
{
    for(JsonImageEffect::ImageEffectInfo* ImageClipInfoItem : ImageEffectJsonData->JsonImageEffectArray)
    {
        std::string StartFuncString = ImageClipInfoItem->StartTimeFunc;
        std::string FinishFuncString = ImageClipInfoItem->FinishTimeFunc;
        std::string ZoomRateFuncString = ImageClipInfoItem->ZoomRateFunc;
        float CurrentBeginTime = ImageClipInfoItem->BeginTime;
        float CurrentEndTime = ImageClipInfoItem->EndTime;

        CalcZoomRateFunc(VideoJsonData.JsonVideoClipArray, ZoomRateFuncString, &ImageClipInfoItem->ZoomRate);
        CalcStartTimeFunc(VideoJsonData.JsonVideoClipArray, StartFuncString, &ImageClipInfoItem->PositionTime);

        float SrcDurationTime = CurrentEndTime - CurrentBeginTime;
        CalcFinishTimeFunc(VideoJsonData.JsonVideoClipArray, ImageClipInfoItem->PositionTime, SrcDurationTime, FinishFuncString, &ImageClipInfoItem->ZoomRate);
    }
}

void TemplateTimeLine::ResetTemplateFontEffectClip(const JsonVideoClip& VideoJsonData, const JsonFontEffect* const& FontEffectJsonData)
{
    for(JsonFontEffect::FontEffectInfo* FontClipInfoItem : FontEffectJsonData->JsonFontEffectArray)
    {
        std::string StartFuncString = FontClipInfoItem->StartTimeFunc;
        std::string FinishFuncString = FontClipInfoItem->FinishTimeFunc;
        std::string ZoomRateFuncString = FontClipInfoItem->ZoomRateFunc;
        float CurrentBeginTime = FontClipInfoItem->BeginTime;
        float CurrentEndTime = FontClipInfoItem->EndTime;

        CalcZoomRateFunc(VideoJsonData.JsonVideoClipArray, ZoomRateFuncString, &FontClipInfoItem->ZoomRate);
        CalcStartTimeFunc(VideoJsonData.JsonVideoClipArray, StartFuncString, &FontClipInfoItem->PositionTime);

        float SrcDurationTime = CurrentEndTime - CurrentBeginTime;
        CalcFinishTimeFunc(VideoJsonData.JsonVideoClipArray, FontClipInfoItem->PositionTime, SrcDurationTime, FinishFuncString, &FontClipInfoItem->ZoomRate);
    }
}

void TemplateTimeLine::ResetTemplateCaptionClip(const JsonVideoClip& VideoJsonData, const JsonShotCutCaption* const& CaptionJsonData)
{
    for(JsonShotCutCaption::CaptionInfo* CaptionInfoItem : CaptionJsonData->JsonCaptionInfoArray)
    {
        std::string StartFuncString = CaptionInfoItem->StartTimeFunc;
        std::string FinishFuncString = CaptionInfoItem->FinishTimeFunc;
        std::string ZoomRateFuncString = CaptionInfoItem->ZoomRateFunc;
        float CurrentBeginTime = CaptionInfoItem->BeginTime;
        float CurrentEndTime = CaptionInfoItem->EndTime;

        CalcZoomRateFunc(VideoJsonData.JsonVideoClipArray, ZoomRateFuncString, &CaptionInfoItem->ZoomRate);
        CalcStartTimeFunc(VideoJsonData.JsonVideoClipArray, StartFuncString, &CaptionInfoItem->PositionTime);

        float SrcDurationTime = CurrentEndTime - CurrentBeginTime;
        CalcFinishTimeFunc(VideoJsonData.JsonVideoClipArray, CaptionInfoItem->PositionTime, SrcDurationTime, FinishFuncString, &CaptionInfoItem->ZoomRate);
    }
}


